'''
@Descripttion: 
@version: 
@encoding: utf-8
@Author: qiurongcan
Date: 2025-10-10 16:30:01
LastEditTime: 2025-10-10 18:58:28
'''





class Solution:

    def lowestCommonAncestor(self, root, p, q):

        if not root or root == p or root == q:
            return root
        
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if not left:
            return right
        
        if not right:
            return left
        
        return root



